package com.rms.controller;

import com.alibaba.fastjson.JSONObject;
import com.rms.entity.Board;
import com.rms.service.BoardService;
import com.rms.utils.UserLoginToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @author 胡满意
 * @date 2019/10/28
 */
@RestController
public class BoardController {
    @Autowired
    private BoardService boardService;

    @GetMapping("/board")
    @UserLoginToken
    public JSONObject selectAllBoard(){
        JSONObject object = new JSONObject();
        try {
            List<Board> boardList = boardService.selectAllBoard();
            object.put("boardList",boardList);
            object.put("state","success");
            object.put("message","餐桌列表获取成功");
            return object;
        }catch (Exception e){
            e.printStackTrace();
            object.put("state","error");
            object.put("message","餐桌列表获取失败");
            return object;
        }
    }

    @PostMapping("/board")
    @UserLoginToken
    public JSONObject insertBoard(HttpServletRequest request, HttpServletResponse response,String boardName) throws IOException {
        JSONObject object = new JSONObject();
        try {
            if (boardService.insertBoard(boardName)>0){
                object.put("state","success");
                object.put("message","餐桌添加成功");
                return object;
            }
            object.put("state","error");
            object.put("message","餐桌添加失败");
            return object;
        }catch (Exception e){
            e.printStackTrace();
            object.put("state","error");
            object.put("message","餐桌添加失败");
            return object;
        }
    }
    @PutMapping("/board")
    @UserLoginToken
    public JSONObject updateStatus(Integer boardId,String boardStatus){
        JSONObject object = new JSONObject();
        System.out.println(boardStatus);
        try {
            if ("预定".equals(boardStatus)){
                Date date = new Date();
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
                sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
                if (boardService.orderBoard(boardId,sdf.parse(sdf.format(date)))>0){
                    object.put("state","success");
                    object.put("message","餐桌更新成功");
                    return object;
                }
                object.put("state","error");
                object.put("message","餐桌更新失败");
                return object;
            }
            if (boardService.unsubscribeBoard(boardId)>0){
                object.put("state","success");
                object.put("message","餐桌更新成功");
                return object;
            }
            object.put("state","error");
            object.put("message","餐桌更新失败");
            return object;
        }catch (Exception e){
            e.printStackTrace();
            object.put("state","error");
            object.put("message","餐桌更新失败");
            return object;
        }
    }
    @DeleteMapping("/board")
    @UserLoginToken
    public JSONObject deleteBoard(Integer boardId){
        JSONObject object = new JSONObject();
        System.out.println(boardId);
        try {
            if (boardService.deleteBoard(boardId)>0){
                object.put("state","success");
                object.put("message","餐桌删除成功");
                return object;
            }
            object.put("state","error");
            object.put("message","餐桌删除失败");
            return object;
        }catch (Exception e){
            e.printStackTrace();
            object.put("state","error");
            object.put("message","餐桌删除失败");
            return object;
        }
    }
}
